package com.example.login;

import java.util.Map;

import com.example.login.service.loginService;

import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.InputFilter.LengthFilter;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText et_username;
	private EditText et_passwd;
	private CheckBox cb_rememberpwd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		et_username = (EditText) findViewById(R.id.et_username);
		et_passwd = (EditText) findViewById(R.id.et_pwd);
		cb_rememberpwd = (CheckBox) findViewById(R.id.cb_remember_pwd);
	
		//检查是否有保存的用户名密码
		Map<String, String> map = loginService.getSavedUserInfo(this);
		if (map != null) {
			et_username.setText(map.get("username"));
			et_passwd.setText(map.get("passwd"));
		}
	}

	public void login(View view) {
		String username = et_username.getText().toString().trim();
		String passwd = et_passwd.getText().toString().trim();
		if (TextUtils.isEmpty(username) || TextUtils.isEmpty(passwd)) {
			Toast.makeText(this, "用户名或密码不能为空!", Toast.LENGTH_SHORT).show();
		} else {
			//判断是否需要保存密码
			if (cb_rememberpwd.isChecked()) {
				//保存用户名密码
				boolean result = loginService.saveUserInfo(this, username, passwd);
				
			}
			//登陆    发送消息到服务器 验证  此处跳过服务器
			if ("zhangsan".equals(username) && "123456".equals(passwd)) {
				Toast.makeText(this, "登陆成功!", 0).show();
			} else {
				Toast.makeText(this, "登陆失败 用户名或密码错误!", 0).show();
			}
		}
	}
}